﻿@using Website.CRUDServices
@using Website.Models.Styling
@using Location = Website.Models.Domain.Location
@model Website.Models.ViewModels.ListView<Activity>
@{
    this.ViewBag.Title = "Forretningsudvikling";
    this.Layout = "~/Views/_Shared/_Layout.cshtml";
    List<Guid> criteriaProfiles = this.ViewData["CriteriaProfiles"] as List<Guid> ?? new List<Guid>();
    var criteriaLocation = this.ViewData["CriteriaLocation"] as Guid?;
    List<Guid> criteriaTopics = this.ViewData["CriteriaTopics"] as List<Guid> ?? new List<Guid>();
    DefaultSearchFields defaultFields = this.ViewData["DefaultFields"] as DefaultSearchFields ?? new DefaultSearchFields();
    var allTopics = this.ViewData["Topics"] as List<Topic>;
    var allProfiles = this.ViewData["Profiles"] as List<Profile>;
    var allProjects = this.ViewData["Projects"] as List<Project>;
    var locations = this.ViewData["Locations"] as List<Location>;
}
<h2>Søgeresultat - @ViewBag.Title</h2>

<fieldset class="searchCriterias">
    <legend>Søgning</legend>
    <table>    
        <tr>
            <td>Type:</td><td>@ViewBag.Title</td>
        </tr>
        <tr><td>Antal resultater i alt: </td><td>@Model.TotalRows</td></tr>
        @if (criteriaLocation.HasValue && criteriaLocation.Value != Guid.Empty)
        {
            <tr><td>Lokation: </td><td>@locations.Single(x => x.ID == criteriaLocation.Value).Name</td></tr>
        }
        @if (criteriaTopics.Any())
        {
            <tr><td>Specificeret snak: </td>
                <td>
                    @foreach (Topic topic in allTopics.Where(x => criteriaTopics.Contains(x.ID)).OrderBy(x => x.Title))
                    {
                        @topic.Title
                        <br />
                    }
                </td>
            </tr>
        }
        @if (criteriaProfiles.Any())
        {
            <tr>
                <td>Profil(er):</td>
                <td>
                    @foreach (Profile profile in allProfiles.Where(x => criteriaProfiles.Contains(x.ID)).OrderBy(x => x.Name))
                    {
                        @profile.Name
                        <br />
                    }
                </td>

            </tr>
        }
   
        @Html.Partial("~/Views/Reports/_SearchDefaultFieldsParametersTableTR.cshtml", defaultFields)

    </table>
</fieldset>





<table class="tableView tableWithOnlyLastChild">
    <tr>
        @foreach (TableViewHeaderItem item in this.Model.Header.Items)
        {
            <td>
                @Html.Partial("~/Views/_Shared/TableViewHeader.cshtml", item)
            </td>
        }
        <td></td>
    </tr>
    @if (this.Model != null)
    {
        foreach (Activity act in this.Model.List)
        {
            <tr>
                <td>@string.Format("{0:dd-MM-yyyy}", act.TimeOfActivity)</td>
                <td style="font-size: 0.7em;">
                    @if (act.BDA.Topics.Any())
                    {
                        foreach(var topic in allTopics.Where(x => act.BDA.Topics.Contains(x.ID)).Select(x => x.Title))
                        {
                            @topic
                            <br />
                        }
                    }
                </td>
                <td>
                    @if (act.Commentary.Length < 30)
                    {
                        @act.Commentary
                    }
                    else
                    {
                        @(string.Format("{0}...", act.Commentary.Substring(0, 30)))
                    }

                </td>
                <td style="font-size: 0.7em;">
                    @if (act.Attendees != null && act.Attendees.Any() )
                    {
                        var test1 = allProfiles.Where(x => act.Attendees.Contains(x.ID));
                        foreach(var att in allProfiles.Where(x => act.Attendees.Contains(x.ID)).OrderBy(x => x.Name).Select(x => x.Name))
                        {
                            @att
                            <br />
                        }
                    }
                </td>
                <td style="font-size: 0.7em;">
                    @if(act.OwnerProjects != null && act.OwnerProjects.Any())
                    {
                        foreach (var proj in allProjects.Where(x => act.OwnerProjects.Contains(x.ID)).OrderBy(x => x.Name).Select(x => x.Name))
                        {
                            @proj
                            <br />
                        }
                    }
                </td>
                <td><a href="@Url.Action("ViewBusinessDevelopment", "BusinessDevelopment", new { Id = act.ID })" target="@act.ID.ToString()">
                        <img src="@Url.Content("~/Content/ButtonIcons/notepad_blue.png")" alt="Link"/></a></td>
            </tr>
        }
    }
    <tr><td colspan="@(1+ Model.Header.Items.Count())"></td></tr>
</table>